/**
 *
 */
package com.mmlc.network.filter;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.httputil.EntryPageName;
import com.mmlc.meimei.lichengkongjian.been.WaitPayBean;
import com.mmlc.network.requestbean.ShoppingCartListRequestBean;
import com.mmlc.network.responsebean.ShoppingCarListResponseBean;
import com.mmlc.utils.DataReflect;
import com.mmlc.utils.PhoneUtils;
import com.mmlc.utils.ReflectToBeanUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 何强
 *         2015-12-18
 *     购物车列表
 */
public class ShoppingCartListRequestFilter extends BaseRequestFilterLayer {
    public ShoppingCartListRequestBean requestBean = new ShoppingCartListRequestBean();

    public ShoppingCartListRequestFilter(BaseParentActivity context) {
        super(context);
        requestBean.method = "myCartList";
        requestBean.imei = PhoneUtils.getIMEI(context);
        makeRequestParams.entryPageName = EntryPageName.n4_a + "";
        makeRequestParams.setReqeustBean(requestBean, true);
    }

    public List<RequestBean> bean1;

    public ShoppingCarListResponseBean resBean;
    public List<ShoppingCarListResponseBean.DataBean.ListBean> shopLlist = new ArrayList<>();
    public int couponNum;//有优惠券的数量
    @Override
    public void getResultSuccess(String responseString, Object obj) {
        bean1 = (List<RequestBean>)ReflectToBeanUtils.reflectJsonToList(RequestBean.class, responseString, 0);

        shopLlist.clear();
        resBean = (ShoppingCarListResponseBean) obj;
        List<ShoppingCarListResponseBean.DataBean.ListBean> tmpShopList = resBean.getData().getList();

        //有优惠券
        List<ShoppingCarListResponseBean.DataBean.ListBean> list1 = new ArrayList<>();
        //无优惠券
        List<ShoppingCarListResponseBean.DataBean.ListBean> list2 = new ArrayList<>();

        for(int i=0;i<tmpShopList.size();i++){
            if(tmpShopList.get(i).getIs_coupon() == 1){//有优惠券
                list1.add(tmpShopList.get(i));
            }else{//无优惠券
                list2.add(tmpShopList.get(i));
            }
        }
        //有优惠卷在前，无优惠卷在后
        shopLlist.addAll(list1);
        shopLlist.addAll(list2);
        couponNum = list1.size();
        parseData(responseString);
    }

    public WaitPayBean responseBean;
    private void parseData(String string) {
        responseBean = new WaitPayBean();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(string);

            // 返回json的数组
            JSONObject j1 = jsonObject.getJSONObject("data");

            responseBean.totalPrice = j1.optString("total_price","");

            List<WaitPayBean.PayBean> beanList = new ArrayList<>();
           /* JSONArray list = j1.getJSONArray("list");
            for (int i = 0; i < list.length(); i++) {
                WaitPayBean.PayBean traceBean = new WaitPayBean.PayBean();

                JSONObject tmp = list.getJSONObject(i);

                if(tmp.optInt("is_row",0) == 1){
                    traceBean.isChecked = true;
                }else{
                    traceBean.isChecked = false;
                }

                traceBean.photo = tmp.optString("img_thumb","");
                traceBean.shopInfo = tmp.optString("name","");
                traceBean.shopColor = tmp.optString("spec","");
                traceBean.shopPrice = tmp.optString("price","");
                traceBean.shopCount = tmp.optString("num","");
                traceBean.goodsId = tmp.optString("goods_id","");
                traceBean.id = tmp.optString("id","");
                beanList.add(traceBean);
            }*/

            for(int i=0;i<shopLlist.size();i++){
                ShoppingCarListResponseBean.DataBean.ListBean jsonBean = shopLlist.get(i);
                WaitPayBean.PayBean traceBean = new WaitPayBean.PayBean();
                if(Integer.valueOf(jsonBean.getIs_row()) == 1){
                    traceBean.isChecked = true;
                }else{
                    traceBean.isChecked = false;
                }

                traceBean.photo = jsonBean.getImg_thumb();
                traceBean.shopInfo = jsonBean.getName();
                traceBean.shopColor = jsonBean.getSpec();
                traceBean.shopPrice = jsonBean.getPrice();
                traceBean.shopCount =jsonBean.getNum();
                traceBean.goodsId = jsonBean.getGoods_id();
                traceBean.id = jsonBean.getId();
                traceBean.type = jsonBean.getType();
                traceBean.market_price = jsonBean.getMarket_price();
                beanList.add(traceBean);
            }
            responseBean.payBean = beanList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @DataReflect(keyFiled={"null,null=havaCoupon",
            "null,null=havaCoupon",

    })
    public class RequestBean{
        //是否有优惠券
        public int havaCoupon;//0，无，1有
    }
}
